﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowStatistics.xaml
    /// </summary>
    public partial class WindowStatistics : Window
    {
        MainWindow window;

        public WindowStatistics()
        {
            InitializeComponent();
        }

        public WindowStatistics(MainWindow window)
        {
            InitializeComponent();
            this.window = window;

            
        }

        private void InitializeGridStatiscics()
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                lblMessage.Content = "";

                int i = 0;
                foreach (StatisticEnum s in Enum.GetValues(typeof(StatisticEnum)))
                {

                    CheckBox ch = new CheckBox();

                    ch.Name = s.ToString();
                    ch.Content = s.toString(); ;

                    ch.Margin = new Thickness(0, i * 30, 0, 0);

                    gridStatistics.Children.Add(ch);
                    i++;
                }

                if (window != null)
                {
                    foreach (Control c in gridStatistics.Children)
                    {
                        if (c is CheckBox)
                        {
                            CheckBox ch = c as CheckBox;

                            StatisticEnum st = window.SelectedStatistics.SingleOrDefault(en => en.ToString() == ch.Name);
                            if ( st > 0 )
                            {
                                ch.IsChecked = true;
                            }

                        }
                    }

                    tbhour1.Text = window.Wtlt.Hours.ToString();
                    tbhour2.Text = window.Itlt.Hours.ToString();
                    tbminute1.Text = window.Wtlt.Minutes.ToString();
                    tbminute2.Text = window.Itlt.Minutes.ToString();
                    tbsecond1.Text = window.Wtlt.Seconds.ToString();
                    tbsecond2.Text = window.Itlt.Seconds.ToString();
                }
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                window.SelectedStatistics.Clear();

                foreach (Control c in gridStatistics.Children)
                {
                    if (c is CheckBox)
                    {
                        CheckBox ch = c as CheckBox;

                        if (ch.IsChecked == true)
                        {
                            foreach (StatisticEnum s in Enum.GetValues(typeof(StatisticEnum)))
                            {
                                if (s.ToString() == ch.Name)
                                {
                                    window.SelectedStatistics.Add(s);
                                }
                            } 
                        }

                    }
                }

                int h1 = 0;
                int h2 = 0;
                int m1 = 0;
                int m2 = 0;
                int s1 = 0;
                int s2 = 0;

                Int32.TryParse(tbhour1.Text, out h1);
                Int32.TryParse(tbhour2.Text, out h2);
                Int32.TryParse(tbminute1.Text, out m1);
                Int32.TryParse(tbminute2.Text, out m2);
                Int32.TryParse(tbsecond1.Text, out s1);
                Int32.TryParse(tbsecond2.Text, out s2);

                window.Wtlt = new TimeSpan(h1, m1, s1);
                window.Itlt = new TimeSpan(h2, m2, s2);

                lblMessage.Content = "Statistics options changed successfully.";
                lblMessage.Foreground = Brushes.Green;

            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.BorderBrush = Brushes.Red;
            }
        }


    }
}
